package com.culture.java.online.education.platform.common.base.response;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {

    private Integer code;

    private String message;

    private T data;

    // 传统的写法
//    public static <T> R<T> success(T data) {
//        return new R<T>().setCode(200).setMessage("成功").setData(data);
//    }
    // 有枚举类的写法
    public static <T> R<T> success(T data) {
        return new R<T>()
                .setCode(Code.SUCCESS.getCode())
                .setMessage(Code.SUCCESS.getMessage())
                .setData(data);
    }

    public static <T> R<T> success() {
        return success(null);
    }

//    public static <T> R<T> failure(Integer code, String message, T data) {
//        return new R<T>().setCode(code).setMessage(message).setData(data);
//    }
    public static <T> R<T> failure(Code code, T data) {
        return new R<T>()
                .setCode(code.getCode())
                .setMessage(code.getMessage())
                .setData(data);
    }

//    public static <T> R<T> failure(Integer code, String message) {
//        return new R<T>().setCode(code).setMessage(message).setData(null);
//    }
    public static <T> R<T> failure(Code code) {
//        return new R<T>()
//                .setCode(code.getCode())
//                .setMessage(code.getMessage())
//                .setData(null);
        // 简化写法
        return failure(code, null);
    }

}
